﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SHKF.B2C.BaseHosted;
using SHKF.B2C.BLL.WorkingCenter;
using SHKF.B2C.Hosted;
using SHKF.B2C.Model.Dto;
using SHKF.B2C.Model.Dto.WorkingCenter;
using SHKF.B2C.Model.Entity.WorkingCenter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SHKF.B2C.Mannage.Service.Controllers
{
    /// <summary>
    /// 分仓api
    /// </summary>
    [Route("[controller]")]
    [ApiController]
    public class ShopRelatedController : BaseController
    {
        private readonly ShopRelatedBLL shopRelatedBLL;

        public ShopRelatedController(ShopRelatedBLL shopRelatedBLL,
           ILogger<ShopRelatedController> logger) : base(logger)
        {
            this.shopRelatedBLL = shopRelatedBLL;
        }

        [HttpPost("Save")]
        public ShopRelatedDto Save(ShopRelatedEntity info)
        {
            ShopRelatedDto dto = new ShopRelatedDto();
            try
            {
                dto.data = this.shopRelatedBLL.Save(info);
            }
            catch (Exception ex)
            {
                dto = new ShopRelatedDto();
                dto.SetException(ex);
                base.WriteException("ShopRelated=>Save", ex);
            }

            return dto;
        }

        [HttpGet("Delete")]
        public DataDto Delete(int SN)
        {
            DataDto dto = new DataDto();
            try
            {
                int iResult = this.shopRelatedBLL.Delete(SN);
                if (iResult == 0)
                {
                    dto.error = new Error()
                    {
                        code = -1,
                        msg = "成功行数为0"
                    };
                }
            }
            catch (Exception ex)
            {
                dto = new DataDto();
                dto.SetException(ex);
                base.WriteException("ShopRelated>>Save", ex);
            }

            return dto;
        }

        [HttpGet("List")]
        public ShopRelatedListDto List(int ShopVSN)
        {
            ShopRelatedListDto dto = new ShopRelatedListDto();
            try
            {
                dto.data = this.shopRelatedBLL.QueryList(ShopVSN);
            }
            catch (Exception ex)
            {
                dto = new ShopRelatedListDto();
                dto.SetException(ex);
                base.WriteException("ShopRelated>>List", ex);
            }

            return dto;
        }

        /// <summary>
        /// 用于保存虚拟分店时
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        [HttpPost("BatchUpdate")]
        public DataDto BatchUpdate(List<ShopRelatedEntity> list)
        {
            DataDto dto = new DataDto();
            try
            {
                foreach(ShopRelatedEntity info in list)
                {
                     this.shopRelatedBLL.Save(info);
                } 
            }
            catch (Exception ex)
            {
                dto = new DataDto();
                dto.SetException(ex);
                base.WriteException("ShopRelated>>Save", ex);
            }

            return dto;
        }
    }
}
